<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/24 0024
 * Time: 16:14
 */

namespace app\wap\controller;


use think\Db;

class Center extends Middle
{
    //todo 获取个人信息
    public function UserInfo(){
        $map = ['uid'=>$this->uid];
        $user = Db::name('user')->where($map)->find();
        $ref = [
            'vip'   =>$this->vip,
            'expire'=>$this->vip_expire,
            'nickname'=>$user['phone'],
            'headimg' =>$user['headimg'],
        ];
        return $this->returnSuccess($ref);
    }
    //todo 获取充值档位
    public function GetGearList(){
      	$this->redis->delete($this->Key);
        $list  = unserialize($this->redis->get($this->Key));
        if(!$list){
            $list = Db::name('gear')
                ->field('id,price,desc,type')
                ->select();
            if(!$list)return $this->returnError($this->NoData);
            $this->redis->set($this->Key,serialize($list));
        }
        $res = [];
        foreach ($list as $k=>$v){
            if($v['type'] == $this->data['type'])array_push($res,$v);
        }
        return $this->returnSuccess($res);
    }
    //todo 获取未读消息数量
    public function CountMessage(){
        $model = [1,2,3,4,5,6,7];
        $array = [];
        foreach ($model as $k){
            $map = ['a.uid'=>$this->uid,'a.read'=>0,'model'=>$k];
            $list = Db::name('message_user')
                ->alias('a')
                ->join('message b','a.me_id=b.me_id')
                ->where($map)
                ->count();
            $ref = ['model'=>$k,'count'=>$list];
            array_push($array,$ref);
        }
        return $this->returnSuccess($array);
    }
    //todo 获取消息列表
    public function GetMsg(){
        $page = $this->page($this->data);
        $map = ['b.uid'=>$this->uid,'a.model'=>$this->data['model']];
        $list = Db::name('message')
            ->alias('a')
            ->join('message_user b','a.me_id=b.me_id')
            ->field('a.content,a.time,a.title,b.id')
            ->order('time desc')
            ->where($map)
            ->limit($page['page'],$page['limit'])
            ->select();
        $map['b.read'] = 0;
        $id = Db::name('message')
            ->alias('a')
            ->join('message_user b','a.me_id=b.me_id')
            ->order('time desc')
            ->where($map)
            ->column('b.id');
        if($id){
            Db::name('message_user')->where(['uid'=>$this->uid,'id'=>['in',$id]])->update(['read'=>1]);
        }
      	foreach($list as $k=>$v){
        	$list[$k]['content'] =  str_replace("\r\n",'',$v['content']);
        }
        return $this->returnSuccess($list);
    }
    //todo 用户邀请码
    public function UserInvitation(){
        return $this->returnSuccess($this->invitation);
    }
    //todo 智能选币
    public function ZhiNeng(){
        $content = Db::name('common')
                ->where(['id'=>18])
                ->value('content');
        $user = Db::name('user_service')->where(['uid'=>$this->uid])->value('zn');
        return $this->returnSuccess(['user'=>$user,'content'=>$content]);
    }

    public function NiuXiong(){
        $content = Db::name('common')
                ->where(['id'=>19])
                ->value('content');
        $user = Db::name('user_service')->where(['uid'=>$this->uid])->value('nx');
        return $this->returnSuccess(['user'=>$user,'content'=>$content]);
    }

    public function Sound(){
        $list = Db::name('common')
          ->where(['type'=>84])
          ->find();
        return $this->returnSuccess($list);

    }

    public function MsgSet(){
        $sound = isset($this->data['sound']) ? $this->data['sound'] : 0;
        $set = [
            'uid'   =>$this->uid,
            'msg'   =>$this->data['msg'],
            'stime' =>$this->data['stime'],
            'etime' =>$this->data['etime'],
            'sound' =>$sound,
        ];
        $res = Db::name('user_msg')->update($set);
        if($res)return $this->returnSuccess(null,'设置成功');
        return $this->returnError($this->Error);

    }

    public function MsgInfo(){
        $map = ['uid'=>$this->uid];
        $info = Db::name('user_msg')->where($map)->find();
        return $this->returnSuccess($info);
    }

    //todo 服务订阅
    public function ServiceSet(){
      	if($this->vip == 0)return $this->returnError('请先订阅会员服务');
      	if($this->vip != 2 && $this->vip_expire < time())return $this->returnError('服务已到期,请续费');
        $map= ['uid'=>$this->uid];
        $service = Db::name('user_service')->where($map)->find();
        $type = $this->data['x'];
      	if($type == 'cl'){
        	if($this->vip != 2)return $this->returnError('请订阅12个月或永久会员服务');
      		if($this->vip_expire < time() && $this->vip_expire > 1)return $this->returnError('服务已到期,请续费');
        }
        if($service[$type] == 1)$map[$type] = 0;
        if($service[$type] == 0)$map[$type] = 1;
        $ref = Db::name('user_service')->update($map);
        if($ref)return $this->returnSuccess(null,'操作成功');
        return $this->returnError($this->Error);
    }
  
  	public function LoginOut(){
        $set = [
            'uid'   =>$this->uid,
            'reg_id'=>null,
        ];
        $res = Db::name('user')->update($set);
        if($res)return $this->returnSuccess(null,'注销成功');
        return $this->returnError($this->Error);
    }
}